﻿<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=utf-8"/>
<title>Тесты источника данных</title>
<script type="text/javascript" src="lib/jsUnit.js"></script>
<script type="text/javascript" src="lib/jsflow.js"></script>
<script type="text/javascript" src="../ui/jquery.min.js"></script>
<script type="text/javascript" src="../js/lib/jspath.js"></script>

<script type="text/javascript" src="../js/dataSources/iDataSource.js"></script>
<script type="text/javascript" src="mockArundo.js"></script>
<script type="text/javascript" src="../js/dataSources/test.js"></script>
<script type="text/javascript">

new JSUnit.Test("Подключение модуля Arundo", "Configuration").run = function(){
	this.assert(typeof(Arundo), "object");
	this.assert(typeof(Arundo.dataSource), "object");
}

new JSUnit.Test("Получение поддерева", "getCatalogTree").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getCatalogTree({rootID:"persons"}, function(data){
			_.assert(data, [{id:"persons", text:"Персоналии", priority:100, children:[
				{id:"management", text:"Руководство", priority:100},
				{id:"developers", text:"Отдел разработки", priority:90},
				{id:"accDep", text:"Бухгалтерия", priority:80},
				{id:"economy", text:"АХО", priority:70}
			]}]);
			go();
		})
	}).run();
}}

new JSUnit.Test("Поддерево 1-го уровня", "getCatalogTree").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getCatalogTree({rootID:null, depth:1}, function(data){
			_.assert(data, [
				{id:"persons", text:"Персоналии"},
				{id:"news", text:"Новости"},
				{id:"goods", text:"Товары"}
			]);
			go();
		})
	}).run();
}}

new JSUnit.Test("Исключение поддерева", "getCatalogTree").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getCatalogTree({rootID:null, excludeBranch:"persons"}, function(data){
			_.assert(data, [
				{id:"news", text:"Новости", priority:80},
				{id:"goods", text:"Товары", priority:60}
			]);
			go();
		})
	}).run();
}}

new JSUnit.Test("Добавление корневого уровня", "getCatalogTree").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getCatalogTree({rootID:null, includeRoot:true, depth:1}, function(data){
			_.assert(data, [
				{id:null, text:"/"},
				{id:"persons", text:"Персоналии"},
				{id:"news", text:"Новости"},
				{id:"goods", text:"Товары"}
			]);
			go();
		})
	}).run();
}}

new JSUnit.Test("getRecord", "Records").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getRecord("PPS", "persons", function(data){
			_.assert(data, {
				columns:{
					id:{field:"id", title:"Row ID", type:"rowID"},
					fio:{field:"fio", title:"ФИО", type:"text"}
				},
				data:{id:"PPS", fio:"Петренко П.С."}
			});
			go();
		})
	}).run();
}}

new JSUnit.Test("saveRecord", "Records").run = function(){with(JSFlow){var _=this;
	sequence(
		function(){var go = new Continuation();
			Arundo.dataSource.saveRecord("PPS", "persons", {fio:"Петренков П.С."}, go);
		},
		function(){var go = new Continuation();
			// проверим, что сохранилось
			Arundo.dataSource.getRecord("PPS", "persons", function(data){
				_.assert(data.data.fio, "Петренков П.С.");
				go();
			});
		}
	).run();
}}

new JSUnit.Test("deleteRows", "Records").run = function(){with(JSFlow){var _=this;
	sequence(
		function(){var go = new Continuation();
			Arundo.dataSource.getTable({catID:"management"}, function(data){
				_.assert(data.length, 2);
				go();
			});
		},
		function(){var go = new Continuation();
			Arundo.dataSource.deleteRows(["III","PPP"], go);
		},
		function(){var go = new Continuation();
			Arundo.dataSource.getTable({catID:"management"}, function(data){
				_.assert(data.length, 0);
				go();
			});
		}
	).run();
}}

new JSUnit.Test("getTable", "Tables").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getTable({catID:"developers"}, function(data){
			_.assert(data, [
				{id:"SSS", fio:"Сидоров С.С."}
			]);
			go();
		});
	}).run();
}}

new JSUnit.Test("getTableColumns", "Tables").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getTableColumns("developers", function(data){
			_.assert(data,  [
				{field:"id", title:"Row ID"},
				{field:"fio", title:"ФИО"}
			]);
			go();
		});
	}).run();
}}

new JSUnit.Test("getAllColumns", "Tables").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getAllColumns("developers", function(data){
			_.assert(data,  [
				{field:"id", title:"Row ID", type:"rowID"},
				{field:"fio", title:"ФИО", type:"text"}
			]);
			go();
		});
	}).run();
}}

new JSUnit.Test("deleteCatalog", "Catalogs").run = function(){with(JSFlow){var _=this;
	sequence(
		function(){var go=new Continuation();
			Arundo.dataSource.getCatalogTree("persons", function(data){
				_.assert(data[0].children.length, 4);
				go();
			});
		},
		function(){var go = new Continuation();
			Arundo.dataSource.deleteCatalog("developers", go);
		},
		function(){var go=new Continuation();
			Arundo.dataSource.getCatalogTree("persons", function(data){
				_.assert(data[0].children.length, 3);
				go();
			});
		}
	).run();
}}

new JSUnit.Test("getCatalogProperties", "Catalogs").run = function(){with(JSFlow){var _=this;
	sequence(function(){var go = new Continuation();
		Arundo.dataSource.getCatalogProperties({catID:"persons"}, function(data){
			_.assert(data.total, 4);
			_.assert(data.rows[0], {name:"ID", value:"persons", hidden:true, editor:"none"});
			_.assert(data.rows[1].name, "Parent");
			_.assert(data.rows[1].value, null);
			_.assert(data.rows[1].editor.type, "combotree");
			_.assert(data.rows[2], {name:"Name", value:"Персоналии", editor:"text"});
			_.assert(data.rows[3], {name:"Priority", value:100, editor:"text"});
			go();
		});
	}).run();
}}

new JSUnit.Test("saveCatalogProperties", "Catalogs").run = function(){with(JSFlow){var _=this;
	sequence(
		function(){var go=new Continuation();
			Arundo.dataSource.getCatalogProperties({catID:"persons"}, function(data){
				_.assert(data.rows[2].value, "Персоналии");
				go();
			});
		},
		function(){var go=new Continuation();
			Arundo.dataSource.saveCatalogProperties("persons", [{name:"Name", value:"Персоналии1"}], go);
		},
		function(){var go=new Continuation();
			Arundo.dataSource.getCatalogProperties({catID:"persons"}, function(data){
				_.assert(data.rows[2].value, "Персоналии1");
				go();
			});
		}
	).run();
}}

function init(){
	IDataSource.check(Arundo.dataSource);
	JSUnit.init();
}
</script>

</head>
<body onload="init()">
	<h1>Тесты источника данных</h1>
	<div id="out"></div>
</body>
</html>